<?php


namespace App\Model\Logic;

use App\Helper\RedisConst;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Redis\Pool;

/**
 * @Bean()
 * 收藏逻辑
 * Class CollectLogic
 * @package App\Model\Logic
 */
class CollectLogic
{

    /**
     * @Inject("redis1.pool")
     * @var Pool
     */
    private $redis;

    /**
     * 批量获取收藏状态
     * @param $member_id
     * @param $post_ids
     * @return array
     */
    public function multiCollection($member_id, $post_ids)
    {
        $func = function($pipe) use($member_id, $post_ids){
            foreach( $post_ids as $post_id)
            {
                $key = RedisConst::POSTPCOLLECT.$member_id;
                $pipe->SISMEMBER($key, $post_id);
            }
        };
        return $this->redis->pipeline($func);
    }

    //判断是否处于集合以内
    public function isMembers($member_id, $post_id)
    {
        $key = RedisConst::POSTPCOLLECT.$member_id;
        $exist = $this->redis->exists($key);
        if(!$exist) return false;
        return $this->redis->sisMember($key, $post_id);
    }

}